
/**
 * 上传文件类
 * @param upload_id
 * @param queueID
 * @param fileObjName
 * @param basePath
 * @param sessionId
 * @param fileTypeExts
 * @param fileSizeLimit
 * @param action
 * @returns {uploadify}
 */
function uploadify (upload_id,queueID,fileObjName
		,basePath,sessionId,fileTypeExts
		,fileSizeLimit,action,fileTypeDesc){
	this.upload_id = upload_id;
	this.queueID = queueID;
	this.fileObjName = fileObjName;
	this.basePath = basePath;
	this.action = action;
	this.sessionId = sessionId;
	this.fileTypeExts = fileTypeExts;
	this.fileSizeLimit = fileSizeLimit;
	this.fileTypeDesc = fileTypeDesc;
	//private
	function getQueueID(_queueID){
		var queueID = "false";
		if(_queueID!=""&&_queueID!=undefined){
			queueID = _queueID;
		}
		return queueID;
    } 
	this.queueID = getQueueID(queueID);
	this.setting = init(this);
	//private
	function init(me){
		   var setting = {
		        //开启调试
		        'debug' : false
		        //是否自动上传
		       , 'auto':false
		       , 'successTimeout' : 30
		        //附带值
		        ,'formData':{
	 	        	'uploadLimit':me.fileSizeLimit
 		            ,'extension':me.fileTypeExts
//		            ,'username':'用户名'
//		            ,'rnd':'加密密文'
		        }
		       , 'method'   : 'post'
		    	//上传处理程序
		    	,'uploader': me.basePath+me.action+";jsessionid="+me.sessionId
		        //flash
		        ,'swf': me.basePath+'resources/fw/plugin/upload/uploadify/uploadify.swf'
		        //,'checkExisting' : me.basePath+'resources/js/lib/upload/uploadify/check-exists.php'
		        //不执行默认的事件
		       // ,'overrideEvents' : ['onDialogClose'],
		        //文件选择后的容器ID
		        ,'queueID': me.queueID//false
		        //服务器端脚本使用的文件对象的名称 $_FILES个['upload']
		        ,'fileObjName':me.fileObjName
		        //,'buttonClass' : '',
		       , 'buttonCursor' : 'hand'
		        //浏览按钮的背景图片路径
		        //,'buttonImage':'upbutton.gif',
		        ,'buttonText' : 'BROWSE...'
		        //浏览按钮的宽度
		       , 'width':'100'
		        //浏览按钮的高度
		        ,'height':'32'
		        //在浏览窗口底部的文件类型下拉菜单中显示的文本
		        ,'fileTypeDesc':me.fileTypeDesc
		        //允许上传的文件后缀
		       , 'fileTypeExts':me.fileTypeExts
		        //上传文件的大小限制
		        ,'fileSizeLimit':me.fileSizeLimit// (B, KB, MB, or GB). The default unit is in KB.You can set this value to 0 for no limit.
		        //上传数量
		       , 'queueSizeLimit' : 25
		        ,'uploadLimit' : 999
		        //,'itemTemplate' : 'false'
		        ,'multi'    : true
		       , 'preventCaching' : true
		        ,'progressData' : 'percentage'//有speed和percentage两种，一个显示速度，一个显示完成百分比 
		       ,'removeCompleted' : true
		       , 'removeTimeout' : 3
		        ,'requeueErrors' : false
		       
		        //每次更新上载的文件的进展
		       , 'onUploadProgress' : onUploadProgress 
		         
		        //选择上传文件后调用
		        ,'onSelect' : onSelect 
		        	 
		        ,'onUploadStart' : onUploadStart 
		        	 
		        //返回一个错误，选择文件的时候触发
		         ,'onSelectError':onSelectError 
		           
		        //检测FLASH失败调用
		        ,'onFallback':onFallback 
		        
		        //上传到服务器，服务器返回相应信息到data里
		        //, 'onUploadSuccess':onUploadSuccess 
		        
		        , 'onUploadComplete':onUploadComplete 
		        	  
		        ,'onUploadError' : onUploadError 
		           
		    };
			$("#"+me.upload_id).uploadify(setting);
			//选择文件的展现
			var itemTemplate = '<div id="${fileID}" class="uploadify-queue-item">';
			 itemTemplate += '<div class="cancel">';
			 itemTemplate += ' <a href="javascript:$(\'#${instanceID}\').uploadify(\'cancel\', \'${fileID}\')">X</a>';
			 itemTemplate += '</div>';
			 itemTemplate += '<span class="fileName">${fileName} (${fileSize})</span><span class="data"></span>';
			 itemTemplate += '</div>';
			 return setting;
		};
};

uploadify.prototype.clearQueue = function(){
	$("#"+this.upload_id).uploadify('cancel','*');
};
uploadify.prototype.cancelFirst = function(){
	$("#"+this.upload_id).uploadify('cancel');
};

uploadify.prototype.cancel = function(id){
	$("#"+this.upload_id).uploadify('cancel',id);
};

uploadify.prototype.upload = function(){
	$("#"+this.upload_id).uploadify('upload','*');
};
uploadify.prototype.stop = function(){
	$("#"+this.upload_id).uploadify('stop');
};
uploadify.prototype.destroy = function (){
	$("#"+this.upload_id).uploadify('destroy');
};
uploadify.prototype.setEnable = function(flag){
	$("#"+this.upload_id).uploadify('disable', flag);
};
uploadify.prototype.setValue = function(name,value) {
    $('#'+this.upload_id).uploadify('settings',name,value);
};
uploadify.prototype.getValue = function(name) {
   return  $('#'+this.upload_id).uploadify('settings',name);
};


/**
 * 添加额外的数据
 * @param postData {}
 */
uploadify.prototype.addFormData = function(postData) {
	   return  this.setValue('formData',postData);
};
	
	
/**************uploadify 事件方法**************/

//每次更新上载的文件的进展
function onUploadProgress(file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) {
     //有时候上传进度什么想自己个性化控制，可以利用这个方法
     //使用方法见官方说明
	 $('#progress').html(totalBytesUploaded + ' bytes uploaded of ' + totalBytesTotal + ' bytes.'+"percent:"+((totalBytesUploaded/totalBytesTotal)*100).toFixed(2)+"%");
};
//选择上传文件后调用
function onSelect(file) {
	 //alert('The file ' + file.name + ' was added to the queue.');
};
function onUploadStart(file) {
	//alert('Starting to upload ' + file.name);
} ;
//返回一个错误，选择文件的时候触发
function onSelectError(file, errorCode, errorMsg){
    switch(errorCode) {
        case -100:
            alert("上传的文件数量已经超出系统限制的"+upload.getValue('queueSizeLimit')+"个文件！");
            break;
        case -110:
            alert("文件 ["+file.name+"] 大小超出系统限制的"+upload.getValue('fileSizeLimit')+"大小！");
            break;
        case -120:
            alert("文件 ["+file.name+"] 大小异常！");
            break;
        case -130:
            alert("文件 ["+file.name+"] 类型不正确！");
            break;
        default:
            alert("文件 ["+file.name+"] 出错！");
            break;
    }
};
//检测FLASH失败调用
function onFallback(){
    alert("您未安装FLASH控件，无法上传图片！请安装FLASH控件后再试。");
};
//上传到服务器，服务器返回相应信息到data里
function onUploadSuccess(file, data, response){
	  //alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
};
function onUploadComplete(file) {
	   // alert('The file ' + file.name + ' finished processing.');
};
function onUploadError(file, errorCode, errorMsg, errorString) {
	 switch(errorCode) {
        case -280:
            break;
        default:
        	 alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
            break;
    }
   
};
function onCancel(file) {
    alert('The file ' + file.name + ' was cancelled.');
};
function onClearQueue (queueItemCount) {
    alert(queueItemCount + ' file(s) were removed from the queue');
} ;


function onDestroy() {
    alert('I am getting destroyed!');
};
function onSWFReady() {
    alert('The Flash file is ready to go.');
} ;
function onQueueComplete(queueData) {
    alert(queueData.uploadsSuccessful + ' files were successfully uploaded.');
};
function onInit(instance) {
    alert('The queue ID is ' + instance.settings.queueID);
};
function onFallback() {
    alert('Flash was not detected.');
};
function onEnable() {
    alert('You can use Uploadify again.');
};
function onDisable() {
    alert('You have disabled Uploadify!');
};
function onDialogOpen() {
    $('#message_box').html('The file dialog box was opened...');
};
function onDialogClose(queueData) {
    alert(queueData.filesQueued + ' files were queued of ' + queueData.filesSelected + ' selected files. There are ' + queueData.queueLength + ' total files in the queue.');
} ;
